<template>
  <div class="error-log">
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form :model="searchForm" ref="searchFormRef" inline>
        <el-form-item label="异常类型" prop="type">
          <el-input
            v-model="searchForm.type"
            placeholder="请输入异常类型"
            clearable
          />
        </el-form-item>
        <el-form-item label="请求方法" prop="method">
          <el-select
            v-model="searchForm.method"
            placeholder="请选择请求方法"
            clearable
            style="width: 120px"
          >
            <el-option label="GET" value="GET" />
            <el-option label="POST" value="POST" />
            <el-option label="PUT" value="PUT" />
            <el-option label="DELETE" value="DELETE" />
          </el-select>
        </el-form-item>
        <el-form-item label="异常时间" prop="timeRange">
          <el-date-picker
            v-model="searchForm.timeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="success" @click="handleExport">导出</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 日志列表 -->
    <el-card class="table-card">
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="type" label="异常类型" width="200" />
        <el-table-column prop="method" label="请求方法" width="100">
          <template #default="scope">
            <el-tag :type="getMethodType(scope.row.method)">
              {{ scope.row.method }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="url" label="请求地址" min-width="200" />
        <el-table-column prop="message" label="异常信息" min-width="300" />
        <el-table-column prop="ip" label="IP地址" width="120" />
        <el-table-column prop="location" label="操作地点" width="150" />
        <el-table-column prop="createTime" label="异常时间" width="180" />
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleDetail(scope.row)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 详情弹窗 -->
    <el-dialog
      v-model="detailVisible"
      title="异常详情"
      width="800px"
      destroy-on-close
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="异常类型">{{ detail.type }}</el-descriptions-item>
        <el-descriptions-item label="请求方法">
          <el-tag :type="getMethodType(detail.method)">
            {{ detail.method }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="请求地址" :span="2">{{ detail.url }}</el-descriptions-item>
        <el-descriptions-item label="IP地址">{{ detail.ip }}</el-descriptions-item>
        <el-descriptions-item label="操作地点">{{ detail.location }}</el-descriptions-item>
        <el-descriptions-item label="异常时间">{{ detail.createTime }}</el-descriptions-item>
        <el-descriptions-item label="操作人">{{ detail.operator }}</el-descriptions-item>
        <el-descriptions-item label="异常信息" :span="2">
          <pre class="error-message">{{ detail.message }}</pre>
        </el-descriptions-item>
        <el-descriptions-item label="异常堆栈" :span="2">
          <pre class="error-message">{{ detail.stackTrace }}</pre>
        </el-descriptions-item>
        <el-descriptions-item label="请求参数" :span="2">
          <pre>{{ detail.requestParams }}</pre>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  type: '',
  method: '',
  timeRange: []
})

// 表格数据
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)
const tableData = ref([
  {
    type: 'NullPointerException',
    method: 'GET',
    url: '/api/user/1',
    message: '空指针异常：用户不存在',
    ip: '192.168.1.1',
    location: '广东省深圳市',
    operator: '管理员',
    createTime: '2024-03-01 10:00:00',
    stackTrace: 'java.lang.NullPointerException\n    at com.example.service.UserService.getUser(UserService.java:42)\n    at com.example.controller.UserController.getUser(UserController.java:25)',
    requestParams: '{"id":1}'
  }
])

// 详情数据
const detailVisible = ref(false)
const detail = ref({})

// 查询
const handleSearch = () => {
  loading.value = true
  // TODO: 实现查询逻辑
  setTimeout(() => {
    loading.value = false
  }, 500)
}

// 重置
const handleReset = () => {
  searchForm.type = ''
  searchForm.method = ''
  searchForm.timeRange = []
  handleSearch()
}

// 导出
const handleExport = () => {
  // TODO: 实现导出逻辑
  ElMessage.success('导出成功')
}

// 详情
const handleDetail = (row) => {
  detail.value = row
  detailVisible.value = true
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}

// 获取请求方法标签类型
const getMethodType = (method) => {
  const map = {
    GET: '',
    POST: 'success',
    PUT: 'warning',
    DELETE: 'danger'
  }
  return map[method]
}
</script>

<style scoped>
.error-log {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-card__body) {
  padding: 20px;
}

pre {
  margin: 0;
  padding: 8px;
  background-color: #f5f7fa;
  border-radius: 4px;
  font-family: monospace;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.error-message {
  color: #f56c6c;
}
</style> 